<template>
  <div class="toast">
    <p :class="type">{{ message }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: "",
      type: "success",
      duration: "2000",
    };
  },
  mounted() {
    // 两秒后自动销毁
    setTimeout(() => {
      // 内置方法，但是不会清除DOM
      this.$destroy();
      this.$el.remove();
    }, this.duration);
  },
};
</script>

<style lang="less" scoped>
@keyframes move {
  0% {
    top: 0;
    opacity: 0;
  }
  100% {
    top: 20px;
    opacity: 1;
  }
}

.toast {
  animation: move 0.5s ease;
  width: 300px;
  height: 60px;
  line-height: 60px;
  position: fixed;
  top: 20px;
  left: 50%;
  margin-left: -150px;
  background: #fff;
  border-radius: 5px;
  p {
    font-size: 14px;
    text-indent: 2em;
  }
}

.success {
  color: #67c23a;
}

.fail {
  color: red;
}

.warning {
  color: #e6a23c;
}
</style>
